
/**   
 * @Title: SDCardFilesAdapter.java 
 * @Package: com.rename.icode.view 
 * @Description: Administrator版权所有
 * @author Administrator  
 * @date 2015-4-16 上午11:19:13 
 * @version  
 */


package com.rename.icode.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.rename.icode.R;
import com.rename.icode.utils.LogUtil;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/** 
 * @Description 实现了...功能
 * @author Administrator
 * @date 2015-4-16 上午11:19:13 
 * @version V1.0
 */

public class SDCardFilesAdapter extends BaseAdapter {

	private static final String TAG = "SDCardFilesAdapter";
	
	private Context context;
	private File[] files;
	private LayoutInflater layoutinfater;
	private OnClickListener onClickListener;
	private boolean[] is_select;
	
	public SDCardFilesAdapter(Context context, File[] files){
		this.context = context;
		this.files = files;
		this.layoutinfater = LayoutInflater.from(context);
		is_select = new boolean[files.length];
		setFalse();
	}
	
	/**
	 * 
	 * @Description 指定某一项的选择状态
	 * @author Administrator
	 * @param p
	 * @param selected
	 */
	public void updateSelection(int p, boolean selected) {
		setFalse();
		is_select[p] = selected;
	}
	
	/**
	 * 
	 * @Description 设置所有项的选择状态
	 * @author Administrator
	 * @param selected
	 */
	public void updateSelection(boolean selected){
		for (int i = 0; i < is_select.length; i++) {
			is_select[i] = selected;
		}
	}
	
	private void setFalse() {
		for (int i = 0; i < is_select.length; i++) {
			is_select[i] = false;
		}
	}
	
	/**
	 * Description 
	 * @return 
	 * @see android.widget.Adapter#getCount() 
	 */

	@Override
	public int getCount() {
		return files.length;
	}

	/**
	 * Description 
	 * @param position
	 * @return 
	 * @see android.widget.Adapter#getItem(int) 
	 */

	@Override
	public Object getItem(int position) {
		return files[position];
	}

	/**
	 * Description 
	 * @param position
	 * @return 
	 * @see android.widget.Adapter#getItemId(int) 
	 */

	@Override
	public long getItemId(int position) {
		return position;
	}

	/**
	 * Description 
	 * @param position
	 * @param convertView
	 * @param parent
	 * @return 
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup) 
	 */

	@SuppressLint("SimpleDateFormat")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		File file = (File)getItem(position);
		View view = null;
		ViewHolder viewHolder = null;
		if(convertView == null){
			view = layoutinfater.inflate(R.layout.sdcard_list_item, null);
			viewHolder = new ViewHolder();
			viewHolder.ivIcon = (ImageView) view.findViewById(R.id.icon);
			viewHolder.text = (TextView) view.findViewById(R.id.file_name);
			viewHolder.tvModify = (TextView) view.findViewById(R.id.file_modify);
			view.setTag(viewHolder);
		} else {
			view = convertView;
			viewHolder = (ViewHolder) view.getTag();
		}
		viewHolder.text.setTag(file);
		viewHolder.text.setText(file.getName());
		if(file.isDirectory()){
			viewHolder.ivIcon.setBackgroundResource(R.drawable.folder);
		}else{
			viewHolder.ivIcon.setBackgroundResource(R.drawable.file);
		}
		
		if(is_select[position]){
			LogUtil.i(TAG, "选中文件是" + viewHolder.text.getText());
			viewHolder.text.setTextColor(context.getResources().getColor(R.color.red));
			viewHolder.tvModify.setTextColor(context.getResources().getColor(R.color.red));
		}else {
			viewHolder.text.setTextColor(context.getResources().getColor(R.color.black));
			viewHolder.tvModify.setTextColor(context.getResources().getColor(R.color.black));
		}
		
		long modTime = file.lastModified();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		viewHolder.tvModify.setText(dateFormat.format(new Date(modTime)));
		
		if(onClickListener != null){
			view.setOnClickListener(onClickListener);
		}
		
		return view;
	}
	
	/**
	 * 
	 * @Description 刷新列表
	 * @author Administrator
	 * @param files
	 */
	public void refreshList(File[] files){
		this.files = files;
		notifyDataSetChanged();
	}
	
	class ViewHolder{
		private ImageView ivIcon;
		private TextView text;
		private TextView tvModify;
	}

	/**
	 * 
	 * @Description 对外暴露点击事件
	 * @author Administrator
	 * @param onClickListener
	 */
	public void setOnclickListener(OnClickListener onClickListener){
		this.onClickListener = onClickListener;
	}
	
}
